package com.greenhouse.business.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSONArray;
import com.greenhouse.business.domain.BizProduct;
import com.greenhouse.business.service.IBizProductService;
import com.greenhouse.common.annotation.Log;
import com.greenhouse.common.core.controller.BaseController;
import com.greenhouse.common.core.domain.AjaxResult;
import com.greenhouse.common.core.page.TableDataInfo;
import com.greenhouse.common.enums.BusinessType;
import com.greenhouse.common.utils.poi.ExcelUtil;

/**
 * 产品管理Controller
 * 
 * @author greenhouse
 * @date 2021-04-12
 */
@RestController
@RequestMapping("/business/product")
public class BizProductController extends BaseController {
	@Autowired
	private IBizProductService bizProductService;

	/**
	 * 查询产品管理列表
	 */
	@PreAuthorize("@ss.hasPermi('business:product:list')")
	@GetMapping("/list")
	public TableDataInfo list(BizProduct bizProduct) {
		startPage();
		List<BizProduct> list = bizProductService.selectBizProductList(bizProduct);
		return getDataTable(list);
	}

	/**
	 * 导出产品管理列表
	 */
	@PreAuthorize("@ss.hasPermi('business:product:export')")
	@Log(title = "产品管理", businessType = BusinessType.EXPORT)
	@GetMapping("/export")
	public AjaxResult export(BizProduct bizProduct) {
		List<BizProduct> list = bizProductService.selectBizProductList(bizProduct);
		ExcelUtil<BizProduct> util = new ExcelUtil<BizProduct>(BizProduct.class);
		return util.exportExcel(list, "product");
	}

	/**
	 * 获取产品管理详细信息
	 */
	@PreAuthorize("@ss.hasPermi('business:product:query')")
	@GetMapping(value = "/{productId}")
	public AjaxResult getInfo(@PathVariable("productId") Long productId) {
		return AjaxResult.success(bizProductService.selectProductVoById(productId));
	}

	/**
	 * 新增产品管理
	 */
	@PreAuthorize("@ss.hasPermi('business:product:add')")
	@Log(title = "产品管理", businessType = BusinessType.INSERT)
	@PostMapping
	public AjaxResult add(@RequestBody BizProduct bizProduct) {
		return toAjax(bizProductService.insertBizProduct(bizProduct));
	}

	/**
	 * 修改产品管理
	 */
	@PreAuthorize("@ss.hasPermi('business:product:edit')")
	@Log(title = "产品管理", businessType = BusinessType.UPDATE)
	@PutMapping
	public AjaxResult edit(@RequestBody BizProduct bizProduct) {
		return toAjax(bizProductService.updateBizProduct(bizProduct));
	}

	/**
	 * 删除产品管理
	 */
	@PreAuthorize("@ss.hasPermi('business:product:remove')")
	@Log(title = "产品管理", businessType = BusinessType.DELETE)
	@DeleteMapping("/{productIds}")
	public AjaxResult remove(@PathVariable Long[] productIds) {
		return toAjax(bizProductService.deleteBizProductByIds(productIds));
	}

	@GetMapping("/table1")
	public AjaxResult getTable1() {

		String str = "[{\"tableName\":\"大棚报警率\",\"data\":[{\"name\":\"报警大棚数\",\"value\":10},{\"name\":\"未告警大棚数\",\"value\":20}]},{\"tableName\":\"设备在线率\",\"data\":[{\"name\":\"离线数量\",\"value\":15},{\"name\":\"在线数量\",\"value\":2}]},{\"tableName\":\"设备告警率\",\"data\":[{\"name\":\"未告警设备数\",\"value\":20},{\"name\":\"告警设备数\",\"value\":10}]}]";

//		String format = String.format(str, inactive, active, offline, online, unWarning, warning);

		return AjaxResult.success(JSONArray.parseArray(str));

	}

	@GetMapping("/table2")
	public AjaxResult getTable2() {

		return AjaxResult.success();
	}
}
